﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using Baula.TextGen.Framework.Tools;

namespace Baula.TextGen.Framework
{
    public class DocumentTemplate 
    {
        public Dictionary<string, PartTemplate> PartTemplates { get; private set; }

        public DocumentTemplate()
            : this(string.Empty)
        { }

        public DocumentTemplate(string content)
        {
            Content = content;
        }

        private void Parse()
        {
            var parser = new TemplateParser(true);

            parser.Parse(Content);

            MainPartTemplate = parser.RootPartTemplate;
            PartTemplates = parser.PartTemplates;
        }

        internal string MainContent
        {
            get { return MainPartTemplate.Content; }
        }

        internal PartTemplate MainPartTemplate { get; private set; }

        internal ReadOnlyCollection<string> Placeholders
        {
            get { return MainPartTemplate.Placeholders; }
        }

        private string _content;
        public string Content
        {
            get { return _content; }
            set { 
                _content = value; 
                Parse();
            }
        }

        public Document CreateDocument()
        {
            return Factory.CreateDocument(this);
        }
    }
}